<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
  <el-header>
    <div>
      <img src="@/img/avater.png" alt="">
      <span>智慧医疗后台管理端</span>
    </div>
    <el-button type="info" @click="logout">退出</el-button>
  </el-header>
  <!-- 页面主体区域 -->

  <el-container>
    <!-- 页面侧边栏 -->
    <el-aside :width="isCollapse ? '64px':'200px' ">
      <!-- 侧边栏菜单区域 -->
      <div class="toggle-button" @click="isCollapse = !isCollapse">|||</div>
      <el-menu background-color="#333744" text-color="#fff" active-text-color="#ffd04b"
      :unique-opened =true :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath"
      >
      <!-- 一级菜单 -->
      <el-submenu :index="index + ''" v-for="(item,index) in menuList" :key="index">
        <template slot="title">
          <i :class="iconsObj[item.id]"></i>
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index="'/'+subItem.path" v-for="(subItem,subKey) in item.children" :key="subKey"
        >
           <i class="el-icon-menu"></i>
           <span slot="title">{{subItem.authName}}</span>
        </el-menu-item>
      </el-submenu>
      </el-menu>
    </el-aside>
    <!--内容主体区域  -->
    <el-main>
      <!-- 陆游站位符 -->
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
// @ is an alias to /src
export default {
  name: "Home",
  data(){
    return {
      menuList:[],
      iconsObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao',
      },
      isCollapse:false,
      activePath:''
    }
  },
  mounted(){
    this.getMenuList();   
  },
  watch:{
    $route:{
      immediate:true,
      handler:function({matched:value}){
        const statePath = value[1].path
        if( statePath== '/welcome') return
        this.activePath = statePath
      }
    }
  },
  methods: {
    logout() {
      window.localStorage.removeItem("token");
      this.$Message.success({ message: "恭喜你,退出成功", center: true });
      this.$router.push("/login");
    },
    async getMenuList(){
       const {data:result} =  await this.$http.get('menus');
       if(result.meta.status !== 200) return this.$Message.error({message:'res.meta.msg',center:true})
       this.menuList = result.data;
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  width: 100%;
  .el-header {
    background-color: #373d41;
    color: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    div {
      display: flex;
      align-items: center;
      img {
        height: 60px;
        widows: 60px;
      }
    }
  }

  .el-aside {
    background-color: #333744;
    i{
      font-size: 20px;
      letter-spacing: 10px;
    }
    .el-menu{
      border: none;
    }
    .toggle-button{
      color: #fff;
      letter-spacing: 0.2rem;
      cursor: pointer;
      line-height: 24px;
      font-size: 10px;
      background: #4a5064;
      text-align: center;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }
}
</style>
